<?php
/**
 * Frontpage controller for JA News Component
 * @package Joomlart
 * @subpackage Component
 */
//Check to ensure this file is included in Joomla
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.controller');

class JanewsControllerfrontpage extends JController
{
	/**
	 * Custom Constructor
	 */
	function __construct( $default = array())
	{
		parent::__construct( $default );

		$this->registerTask( 'apply', 'save' );

		$this->registerTask( 'edit', 'display');
	}

	function display( )
	{
	  switch($this->getTask())
		{
			case 'edit'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'view', 'frontpage'  );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'edit', true );
			} break;

			case 'add' :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'view', 'frontpage'  );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'edit', false );
			} break;
		}

		//Set the default view, just in case
		$view = JRequest::getCmd('view');
		if(empty($view)) {
			JRequest::setVar('layout', 'default');
			JRequest::setVar( 'view', 'frontpage'  );
		}
		parent::display();
	}
	
	function todisplay()
	{
	  $db		= $this->getModel('frontpage');
		$post = JRequest::get('post');
		$cid = $post['cid'];
		$db->updateDefault($cid[0]);
		$msg = JText::_( 'DEFAULT FRONTPAGE LAYOUT SET' );
		$link = 'index.php?option=com_janews&controller=frontpage';
		$this->setRedirect($link);
	}
	
	function save()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$db	= & JFactory::getDBO();

		// save the layout template parent information
		$post	= JRequest::get( 'post' );
		$id = $post['cid'];
		$isNew = ($id == 0);
		
		$obj = new stdClass();
		$obj->title = htmlspecialchars($post['title'], ENT_QUOTES, 'UTF-8');
		$obj->sections = $post['secs'];;
		$obj->headline = $post['headline'];
		$obj->blog = $post['blog'];
		if ($isNew) {
			$db->insertObject('#__janews_frontpage', $obj);
			$query = 'SELECT MAX(id) FROM #__janews_frontpage';
			$db->setQuery($query);
			$id = $db->loadResult();
			$msg = JText::_( 'NEW FRONTPAGE LAYOUT CREATED' );
			$link = 'index.php?option=com_janews&controller=frontpage';
		}
		else {
			$obj->id = $id;
			$db->updateObject('#__janews_frontpage', $obj, 'id');
			$msg = JText::_( 'CHANGES TO FRONTPAGE LAYOUT SAVED' );
			$link = 'index.php?option=com_janews&controller=frontpage';
		}
		if ($this->_task=='apply') {
		  $link = 'index.php?option=com_janews&controller=frontpage&task=edit&cid[]='.$id;
		}
		$this->setRedirect($link, $msg);
	}
	
	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_( 'OPERATION CANCELLED' );
		$this->setRedirect( 'index.php?option=com_janews&controller=frontpage', $msg );
	}
	
	function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$db		=& JFactory::getDBO();
		$cid	= JRequest::getVar( 'cid', array(), '', 'array' );
    JArrayHelper::toInteger($cid);
    
		if (!count($cid)) {
			$this->setRedirect( 'index.php?option=com_janews&controller=frontpage', JText::_('No Items Selected') );
			return false;
		}
		
		$model =& $this->getModel('frontpage');
		$count = 0;

		for ($i=0, $n=count($cid); $i < $n; $i++)
		{
		  if ($model->checkDefault($cid[$i]) != 1) {
		    $tmpl =& JTable::getInstance('janews_frontpage', 'Table');
  			if (!$tmpl->delete( $cid[$i] ))
  				$msg = $tmpl->getError();
				else
				  $count++;
		  }
		  else {
		    JError::raiseWarning( 'SOME_ERROR_CODE', JText::_('YOU CANNOT TRASH THE DEFAULT FRONTPAGE ITEM'));
		  }
		}
		$msg = JText::sprintf( $count.' Item(s) deleted');
		$this->setRedirect( 'index.php?option=com_janews&controller=frontpage', $msg );
		
	}
	
}
?>